package com.zt.controller.servlet;

import com.google.gson.Gson;
import com.zt.business.vo.ResponseData;
import com.zt.controller.annotation.RequestMethod;
import lombok.extern.log4j.Log4j2;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Objects;

@Log4j2
public class BaseServlet extends HttpServlet {

    protected HttpServletRequest request = null;
    protected HttpServletResponse response = null;
    protected HttpSession session = null;
    protected Gson gson = new Gson();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.request = req;
        this.response = resp;
        this.session = req.getSession();

        Class<? extends BaseServlet> clazz = this.getClass();

        //请求方式
        String requestMethod = req.getMethod();
        //请求路径
        String requestURI = req.getRequestURI();
        requestURI = requestURI.split("\\?")[0];

        log.debug(requestURI);

        //请求方法
        Method invokeMethod = null;

        Method[] methods = clazz.getDeclaredMethods();
        for (Method method : methods) {
            RequestMethod annotation = method.getAnnotation(RequestMethod.class);
            if(Objects.isNull(annotation)){
                continue;
            }
            if(requestMethod.equalsIgnoreCase(annotation.method().getCode()) && requestURI.equalsIgnoreCase(annotation.value())){
                invokeMethod = method;
                break;
            }
        }

        if(Objects.isNull(invokeMethod)){
            log.debug("执行方法没有找到，请检查请求方法");
            throw new ServletException("执行方法没有找到，请检查请求方法");
        }

        //使用反射执行目标方法
        try {
            Object obj = invokeMethod.invoke(this);
            if(Objects.nonNull(obj)){
                String url = (String) obj;
                if(url.startsWith("redirect:")){
                    resp.sendRedirect("/page/"+url.split(":")[1]+".jsp");
                }else{
                    req.getRequestDispatcher("/page/"+url+".jsp").forward(req,resp);
                }
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
        protected void responseAjax(ResponseData data){
            response.setContentType("application/json;charset=utf-8");
            PrintWriter out = null;
            try {
                out = response.getWriter();
                out.print(gson.toJson(data));
                out.flush();
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
        }
    }
}
